package cn.caixiong.org.service.Impl;

import cn.caixiong.basic.utils.PageList;
import cn.caixiong.basic.utils.TreeUtil;
import cn.caixiong.org.domain.Department;
import cn.caixiong.org.mapper.DepartmentMapper;
import cn.caixiong.org.query.BaseQuery;
import cn.caixiong.org.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements IDepartmentService {
    //注入依赖
    @Autowired
    private DepartmentMapper departmentMapper;
    //新增或修改
    @Override
    @Transactional
    public void saveOrUpdata( Department department) {
//        if (StringUtils.isBlank(department.getName())){
//           throw new RuntimeException("部门名称不能为空");
//        }
//        if (StringUtils.isBlank(department.getSn())){
//            throw new RuntimeException("部门编号不能为空");
//        }
        if (department.getId()==null){
            departmentMapper.insertSelective(department);
            makeParentDept(department);
            departmentMapper.updateByPrimaryKey(department);
        }else {
            makeParentDept(department);
            departmentMapper.updateByPrimaryKeySelective(department);
        }
    }
    //公共代码
    private void makeParentDept(Department department) {
        Long[] parentIds = department.getParentIds();
        if (parentIds != null && parentIds.length > 0) {
            Long parent = parentIds[parentIds.length - 1];
            department.setParentId(parent);
            StringBuilder buffer = new StringBuilder("");
            //2.循环  用得快捷提示:输入iter
            for (Long Id : parentIds) {
                buffer.append("/").append(Id);
            }
            buffer.append("/").append(department.getId());
            //3.加上自己的主键id值
            department.setDirPath(buffer.toString());
        } else{
            department.setParentId(null);
            department.setDirPath("/" + department.getId());
        }
    }

    //根据id删除
    @Override
    public void deleteId(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }
    //查询所有
    @Override
    public List<Department> findAll() {
        return departmentMapper.selectAll();
    }
    //根据id查询
    @Override
    public void findOne(Long id) {
        departmentMapper.selectByPrimaryKey(id);
    }
    //分页查询
    @Override
    public PageList<Department> findPage(BaseQuery query) {
        PageHelper.startPage(query.getPage(),query.getSize());
        List<Department> departmentList = departmentMapper.findPage(query);
        PageInfo<Department> pageInfo = new PageInfo<>(departmentList);
        //自定义对象
        PageList pageList = new PageList<>(pageInfo.getTotal(),pageInfo.getList());
        return pageList;
    }

    //批量删除
    @Override
    public void deleteAll(Long[] query) {
        departmentMapper.deleteAll(query);
    }
    //查询上级部门
    @Override
    public List<Department> parentDept() {
        return departmentMapper.parentDept();
    }
    //查询级联
    @Override
    public List<Department> treeList() {
        List<Department> all = departmentMapper.selectAll();
        //使用TreeUtil的方法
        List<Department> tree = TreeUtil.getTree(all);
        return tree;
    }
}
